package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController

public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        Emp loginEmp=empService.login(emp);
        if(loginEmp!=null){
            //自定义信息
            Map<String,Object> claims=new HashMap<>();
            claims.put("username",loginEmp.getUsername());
            //使用JWT  工具生成 Token
            String token= JwtUtils.gernerateJWT(claims);
            return Result.success(token);
        }
        return Result.error("用户名或者密码错误");

    }
}
